package keye.document_manage.util;

import java.util.HashMap;
import java.util.List;

import keye.document_manage.entity.TableFiled;

public class SqlServerDialect implements Dialect {

	/**
	 * 结果加两列  tempRowNumber， tempColumn
	 */
	@Override
	public String getPageSql(String sql, long offset, long limit) {
		if(offset <0){
			offset =0;
		}
		if(limit<0){
			limit=0;
		}
		sql = sql.toLowerCase().replaceFirst("select", "select top 100 PERCENT");
		String order = sql.replaceAll(".*order by", " order by");
		StringBuilder sb = new StringBuilder();
		sb.append("select * from ");
		sb.append(" ( select row_number()over(order by tempColumn)tempRowNumber,* ");
		sb.append(" from (select top "+(offset+limit)+" tempColumn=0,* ");
		sb.append(" from ( "+sql+" ) t "+(order.length()==sql.length()?"":order));
		sb.append(" )tt "
				+ " )ttt "
				+ " where tempRowNumber > "+offset);
		System.out.println("getPageSql :"+ sb);
		return sb.toString();
	}

	@Override
	public String changeType(String type) {
		// TODO Auto-generated method stub
		return type;
	}

	@Override
	public String getInsertSql(List<TableFiled> fileds,
			List<HashMap<String, Object>> values) {
		// TODO Auto-generated method stub
		return null;
	}

}
